package com.shuoyue.ycgk.ui.mok;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.text.Html;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.base.BaseMvpActivity;
import com.shuoyue.ycgk.entity.Mok;
import com.shuoyue.ycgk.entity.UserTestPaper;
import com.shuoyue.ycgk.ui.questionbank.practice.mok.MokQuestionActivity;
import com.shuoyue.ycgk.utils.MyRichTextHelper;
import com.shuoyue.ycgk.utils.ShareUtil;
import com.shuoyue.ycgk.utils.XDateUtils;
import com.shuoyue.ycgk.utils.XToast;

import java.util.TimerTask;

import butterknife.BindView;
import butterknife.OnClick;

public class MokDetailActivity extends BaseMvpActivity<MokDetailContract.Presenter>
        implements MokDetailContract.View {


    @BindView(R.id.page_title)
    TextView pageTitle;
    @BindView(R.id.iv_right)
    ImageView ivRight;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.start_time)
    TextView startTime;
    @BindView(R.id.end_time)
    TextView endTime;
    @BindView(R.id.time_total)
    TextView timeTotal;
    @BindView(R.id.total_score)
    TextView totalScore;
    @BindView(R.id.introduct)
    TextView introduct;
    @BindView(R.id.num)
    TextView num;
    @BindView(R.id.statue)
    TextView statue;
    @BindView(R.id.sourt)
    LinearLayout sourt;
    @BindView(R.id.start)
    TextView start;
    Mok info;
    int id;
    long starttimeMul;
    long endTimeMul;

    public static void start(Context context, int mokId) {
        context.startActivity(new Intent(context, MokDetailActivity.class)
                .putExtra("id", mokId));
    }

    @Override
    public void initData() {
        id = getIntent().getIntExtra("id", -1);
        mPresenter = new MokDetailContract.Presenter();
        mPresenter.attachView(this);
//        mPresenter.getMokInfo(id);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_mok_detail;
    }

    @Override
    protected void initView() {
        pageTitle.setText("");
        ivRight.setVisibility(View.VISIBLE);
        ivRight.setImageResource(R.mipmap.share);
    }

    @Override
    public void setMokInfo(Mok info) {
        this.info = info;
        this.title.setText(info.getTitle());
        this.startTime.setText(info.getStartTime());
        this.endTime.setText(info.getEndTime());
        this.timeTotal.setText("共" + info.getMinute()+"分钟");
        this.totalScore.setText("" + info.getTotalQuestion()+"题");
//        this.introduct.setText(info.getExamNote());
        MyRichTextHelper.showRich(mContext, info.getExamNote(), introduct);
        this.num.setText(Html.fromHtml(info.getTotalPerson() + "<font color='#333333'> 人已参加</font> "));
        this.statue.setText(info.getMockStatus());
        this.start.setText(info.getMemberStatus());
        setButtonInfo();

    }

    int state = 0;//标记状态


    private void setButtonInfo() {
        try {
            starttimeMul = XDateUtils.ConverToDateTime(info.getStartTime()).getTime();
            endTimeMul = XDateUtils.ConverToDateTime(info.getEndTime()).getTime();
        } catch (Exception e) {
            e.printStackTrace();
        }
        //:0未开始1已开始2已结束
        if (info.getMockStatusInt() == 0) {
            //未开始
            if (info.getMemberStatusInt() == 0) {
                //显示我要报名报名
                state = 1;
            } else {
                if (info.getCountDown() != 0) {
                    //显示倒计时
                    state = 2;
                    start.setText("");
                    handler.post(clock);
                } else {
                    //显示已报名
                    state = 3;
                    start.setText("已报名");

                }
            }
        } else if (info.getMockStatusInt() == 1) {
            //1已开始
            if (info.getMemberStatusInt() == 0) {
                //显示提示信息,模考已开始,不允许进入
                state = 4;
                start.setText("模考已开始,请下次再来");
            } else {
                //进入考试
                state = 5;
                start.setText("进入考试");
            }
        } else if (info.getMockStatusInt() == 2) {
            //已结束
            //都是静态的状态,可以再这里处理掉
            if (info.getIsDoneSort() == 1) {
                //排名已经出来了,显示排名按钮,显示开始练习
                sourt.setVisibility(View.VISIBLE);
                state = 6;
                start.setText("开始练习");
            } else {
                //显示提示信息,模考已结束，等待排行
                state = 7;
                start.setText("模考已结束，等待排行");
            }
        }
    }

    Handler handler = new Handler();
    TimerTask clock = new TimerTask() {
        @Override
        public void run() {
            //每秒计时
            long nowTime =
                    System.currentTimeMillis();
            long des = starttimeMul - nowTime;//还剩 多少时间开始
            if (des > 0) {
                handler.postDelayed(clock, 1000);//继续跑计时
                start.setText("考试倒计时: " + XDateUtils.formatHHmmss(des));
            } else {
                statue.setText("已开始");
                state = 5;//进入考试
                start.setText("进入考试");
            }
        }
    };

    @Override
    public void registSuc() {
        initData();
    }

    @Override
    public void startSuc(UserTestPaper userTestPaper) {
        MokQuestionActivity.start(mContext, userTestPaper, info.getExamTime() * 1000, state == 5);
    }


    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.getMokInfo(id);
    }
    @OnClick({R.id.back})
    public void onViewClicked() {
        finish();
    }
    @OnClick({  R.id.sourt, R.id.start, R.id.iv_right})
    public void onClick(View view) {
        if(info==null){
            return;
        }
        switch (view.getId()) {

            case R.id.iv_right:
                if (info != null)
                    ShareUtil.shareUrl(this, ShareUtil.MOK_URL + id,
                            info.getTitle(),
                            "",
                            null);
                break;
            case R.id.sourt:
                MokSourtActivity.start(mContext, id);
                break;
            case R.id.start:
                switch (state) {
                    case 1://未开始
                        mPresenter.registMok(id);
                        break;
                    case 5:
                        if (info.getSubmitStatus() == 0) {
                            mPresenter.startMok(info.getIsSign());
                        } else {
                            XToast.toast(mContext, "您已交卷");
                        }
                        break;
                    case 6://已结束,练习
                        if (info.getIsSign() != 0) {
                            mPresenter.startMok(info.getIsSign());
                        } else {
                            XToast.toast(mContext, "没有报名此模考");
                        }
//                        mPresenter.getMokPaper(info.getTestPaperId());
                        break;
                }
                break;
        }
    }

    @Override
    protected void onDestroy() {
        handler.removeCallbacks(clock);
        super.onDestroy();
    }
}
